package com.itheima.server;

import com.itheima.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;

public class ServerThead implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServerThead.class);
    private Socket accept;

    public ServerThead(Socket accept) {
        this.accept = accept;

    }

    @Override
    public void run() {
        synchronized (MainApp.OBJECT) {

            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
                String s = br.readLine();
                char[] chars = s.toCharArray();
                char flag = chars[1];
                switch (flag) {
                    //添加
                    case '1':
                        new StudentService().addStudent(s, bw);
                        break;
                    //修改
                    case '2':
                        new StudentService().updateStudent(s,bw,accept);
                        break;
                    //删除
                    case '3':
                        new StudentService().deleteStudent(s.substring(3),bw,accept);
                        break;
                    //查询所有数据
                    case '4':
                        new StudentService().findAllStudent(accept, bw);
                        break;
                    default:
                        bw.write("输入数据非法!");
                        bw.flush();
                        bw.close();
                        accept.close();
                        LOGGER.error("[客户端]输入数据非法!");
                        break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
